home *** CD-ROM | disk | FTP | other *** search
- // dibdoc.cpp : implementation of the CDibDoc class
- //
- // This is a part of the Microsoft Foundation Classes C++ library.
- // Copyright (C) 1999 Microsoft Corporation
- // All rights reserved.
- //
- // This source code is only intended as a supplement to the
- // Microsoft Foundation Classes Reference and related
- // electronic documentation provided with the library.
- // See these sources for detailed information regarding the
- // Microsoft Foundation Classes product.
-
- #include "stdafx.h"
- #include <limits.h>
- #include "resource.h"
-
- #include "dibdoc.h"
- #include "mainfrm.h"
-
- #ifdef _DEBUG
- #undef THIS_FILE
- static char BASED_CODE THIS_FILE[] = __FILE__;
- #endif
-
- /////////////////////////////////////////////////////////////////////////////
- // CDibDoc
-
- IMPLEMENT_DYNCREATE(CDibDoc, CDocument)
-
- BEGIN_MESSAGE_MAP(CDibDoc, CDocument)
- //{{AFX_MSG_MAP(CDibDoc)
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
-
- /////////////////////////////////////////////////////////////////////////////
- // CDibDoc construction/destruction
-
- CDibDoc::CDibDoc()
- {
- m_hDIB = NULL;
- m_palDIB = NULL;
- m_sizeDoc = CSize(1,1); // dummy value to make CScrollView happy
- }
-
- CDibDoc::~CDibDoc()
- {
- if (m_hDIB != NULL)
- {
- ::GlobalFree((HGLOBAL) m_hDIB);
- }
- if (m_palDIB != NULL)
- {
- delete m_palDIB;
- }
- }
-
- void CDibDoc::InitDIBData()
- {
- if (m_palDIB != NULL)
- {
- delete m_palDIB;
- m_palDIB = NULL;
- }
- if (m_hDIB == NULL)
- {
- return;
- }
- // Set up document size
- LPSTR lpDIB = (LPSTR)m_hDIB;
- if (::DIBWidth(lpDIB) > INT_MAX ||::DIBHeight(lpDIB) > INT_MAX)
- {
- ::GlobalFree((HGLOBAL) m_hDIB);
- m_hDIB = NULL;
- CString strMsg;
- MessageBox(NULL, CString((LPCTSTR)IDS_IMAGE), NULL, MB_ICONINFORMATION | MB_OK);
- return;
- }
- m_sizeDoc = CSize((int) ::DIBWidth(lpDIB), (int) ::DIBHeight(lpDIB));
- // Create copy of palette
- m_palDIB = new CPalette;
- if (m_palDIB == NULL)
- {
- // we must be really low on memory
- ::GlobalFree((HGLOBAL) m_hDIB);
- m_hDIB = NULL;
- return;
- }
- if (::CreateDIBPalette(m_hDIB, m_palDIB) == NULL)
- {
- // DIB may not have a palette
- delete m_palDIB;
- m_palDIB = NULL;
- return;
- }
- }
-
-
- BOOL CDibDoc::OnOpenFile(LPCTSTR lpszPathName)
- {
- CFile file;
- CFileException fe;
-
- #if !defined(READ_BM_FROM_RESOURCE)
- if (!file.Open(lpszPathName, CFile::modeRead | CFile::shareDenyWrite, &fe))
- {
- ReportSaveLoadException(lpszPathName, &fe,
- FALSE, AFX_IDP_FAILED_TO_OPEN_DOC);
- return FALSE;
- }
- #endif
-
- DeleteContents();
- BeginWaitCursor();
-
- // replace calls to Serialize with ReadDIBFile function
- TRY
- {
- m_hDIB = ::ReadDIBFile(file);
- }
- CATCH (CFileException, eLoad)
- {
- file.Abort(); // will not throw an exception
- EndWaitCursor();
- ReportSaveLoadException(lpszPathName, eLoad,
- FALSE, AFX_IDP_FAILED_TO_OPEN_DOC);
- m_hDIB = NULL;
- return FALSE;
- }
- END_CATCH
-
- InitDIBData();
- EndWaitCursor();
-
- if (m_hDIB == NULL)
- {
- // may not be DIB format
- CString strMsg;
- MessageBox(NULL, CString((LPCTSTR)IDS_LOAD), NULL, MB_ICONINFORMATION | MB_OK);
- return FALSE;
- }
- SetPathName(lpszPathName);
- SetModifiedFlag(FALSE); // start off with unmodified
-
- POSITION pos = GetFirstViewPosition();
- CView* pView = GetNextView( pos );
- if(pView)
- pView->SendMessage(WM_USER, (WPARAM)pView->m_hWnd);
-
- return TRUE;
- }
-
- void CDibDoc::ReplaceHDIB(HDIB hDIB)
- {
- if (m_hDIB != NULL)
- {
- ::GlobalFree((HGLOBAL) m_hDIB);
- }
- m_hDIB = hDIB;
- }
-
-
- /////////////////////////////////////////////////////////////////////////////
- // CDibDoc diagnostics
-
- #ifdef _DEBUG
- void CDibDoc::AssertValid() const
- {
- CDocument::AssertValid();
- }
-
- void CDibDoc::Dump(CDumpContext& dc) const
- {
- CDocument::Dump(dc);
- }
-
- #endif //_DEBUG
-
- /////////////////////////////////////////////////////////////////////////////
- // CDibDoc commands
-